< previous page page_344 next page >

Page 344
Youll find that a lot of programmers use two labels in every procedureone for a normal exit of the function and one for the error label. Heres an example:
9f31265bb40b592b1929e18a47ab28e5.gif
Private Sub cmdResumeLabel_Click()
   ' Demonstrate the Resume statement.

   On Error GoTo Error_EH

   Stop

   Screen.MousePointer = vbHourglass

   ' Attempt to open a file on Drive A:
   MsgBox "The length of the file is: " & _
          FileLen(txtFileLen)

NormalExit:
   ' Clean up Code
   Screen.MousePointer = vbDefault
   Exit Sub

Error_EH:
   MsgBox "Error Opening File"
   Resume NormalExit
End Sub
This technique is great if you have some cleanup code you need to perform when exiting the procedure normally or if youre exiting because of an error. Instead of having to put the cleanup code in two places, you can simply put it in the normal exit label. In this example, I simply reset the MousePointer back to a normal cursor instead of the hourglass, which it was set to at the beginning of the procedure. You can view and run the sample code as shown in Figure 17.5 by loading the sample project and try it out.
Resume
In some applications, if an error occurs, you may want to give the user the chance to fix the error. For example, if an error occurs when the user is opening a file on a floppy disk drive, the problem may be that the user has not put the floppy disk in the drive yet. Therefore, you can just ask the user to insert the floppy disk and then try the operation again. Heres the code you would use to handle this situation:

 
< previous page page_344 next page >

If you like this book, buy it!